package cn.edu.zut.internship.service.impl.evaluation;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.iservice.evaluation.EvaluationSV;
import cn.edu.zut.internship.mapper.company.CompanyMapper;
import cn.edu.zut.internship.mapper.evaluation.EvaluationMapper;
import cn.edu.zut.internship.mapper.notify.NotifyMapper;
import cn.edu.zut.internship.utils.PrimaryKeyUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class EvaluationSVImpl implements EvaluationSV {
    @Autowired
    EvaluationMapper evaluationMapper;
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
    @Autowired
    private NotifyMapper notifyMapper;
    @Override
    public Result getEvaluationById(Long evaluationId) {
        Result result = new Result();
        Map evaluation = evaluationMapper.getEvaluation(evaluationId);
        result.setBean(evaluation);
        return result;
    }

    @Override
    public Result selectEvaluation(Map params) {
        Result result = new Result();
        //查询文章所有一级评论
        List<Map> list = evaluationMapper.getList(params);
        //查询一级评论的数量
        int total = evaluationMapper.getListCount(params);
        result.getBean().put("total",total);
        //查询所有评论的子评论
        if(!CollectionUtils.isEmpty(list)) {
            for (Map evaluation : list) {
                getEvaluationChildren(evaluation);
                //参数处理
                //匿名评论
                int evaluationType = (int) evaluation.get("evaluationType");
                if(evaluationType == 0){
                    //处理评论者姓名和头像
                    evaluation.put("userName","匿名");
                    evaluation.put("avatar","https://z3.ax1x.com/2021/05/19/g5GccT.jpg");

                }
            }
        }
        result.setBeans(list);
        return result;
    }

    @Override
    public Result addPersonEvaluation(Map params) {
        Result result = new Result();
        //添加评论
        Long evaluationId = primaryKeyUtil.generatePrimaryKey("evaluation");
        params.put("evaluationId",evaluationId);
        evaluationMapper.addEvaluation(params);
        //更新实训单位评分
        if(params.get("internshipId") instanceof String){
            Long internshipId = Long.valueOf((String) params.get("internshipId"));
        }
        Long internshipId = (Long) params.get("internshipId");
        Map companyScore = evaluationMapper.getCompanyScore(internshipId);
        Double evaluationAvg = (Double) companyScore.get("evaluationAvg");
        NumberFormat nf=new DecimalFormat( "0.0");
        evaluationAvg = Double.parseDouble(nf.format(evaluationAvg));
        Map scoreParams = new HashMap();
        scoreParams.put("score",evaluationAvg);
        scoreParams.put("internshipId",internshipId);
        companyMapper.updateCompanyScore(scoreParams);
        return result;
    }

    @Override
    public Result addSonEvaluation(Map params) {
        Result result = new Result();
        //添加评论
        Long evaluationId = primaryKeyUtil.generatePrimaryKey("evaluation");
        params.put("evaluationId",evaluationId);
        evaluationMapper.addEvaluation(params);
        //给父级评论创建者发送提醒消息
        Long personId = 0L;
        if(params.get("personId") instanceof Long){
             personId = (Long) params.get("personId");
        }else if(params.get("personId") instanceof String){
            String s = (String) params.get("personId");
            if(!StringUtils.isEmpty(s)){
                personId = Long.getLong(s);
            }
        }
        if(personId!=0L) {
            //添加一条提醒消息
            Map<String, Object> notify = new HashMap();
            Long notifyId = primaryKeyUtil.generatePrimaryKey("notify");
            notify.put("notifyId", notifyId);
            notify.put("internshipId", params.get("internshipId"));
            notify.put("sendAccount", params.get("userName"));
            notify.put("targetAccount", params.get("personAccount"));
            notify.put("sendType", 1);
            notify.put("notifyType", 1);
            notify.put("content", "***回复了你的评论" + params.get("text"));
            notify.put("targetType", 1);
            notify.put("creat_account", params.get("userName"));
            notifyMapper.addNotify(notify);
        }
        return result;
    }

    @Override
    public Result delectEvaluation(Map params) {
        Result result = new Result();
        //获取评论信息
        Map evaluation = evaluationMapper.getEvaluation((Long) params.get("evaluationId"));
        //删除此评论
        evaluationMapper.deleteEvaluation(params);
        //删除子评论
        evaluationMapper.deleteChildrenEvaluation(params);
        //更新实训单位评分
        Long internshipId = (Long) evaluation.get("internshipId");
        Map companyScore = evaluationMapper.getCompanyScore(internshipId);
        Double evaluationAvg = (Double) companyScore.get("evaluationAvg");
        NumberFormat nf=new DecimalFormat( "0.0");
        evaluationAvg = Double.parseDouble(nf.format(evaluationAvg));
        Map scoreParams = new HashMap();
        scoreParams.put("score",evaluationAvg);
        scoreParams.put("internshipId",internshipId);
        companyMapper.updateCompanyScore(scoreParams);
        return result;
    }

    @Override
    public Result updataEvaluation(Map params) {
        Result result = new Result();
        evaluationMapper.updateEvaluation(params);
        return result;
    }

    @Override
    public Result addLikeNum(Map params) {
        Result result = new Result();
        //点赞提醒
        Long evaluationId = (Long) params.get("evaluationId");
        Map evaluation = evaluationMapper.getEvaluation(evaluationId);
        String targetAccount = (String) evaluation.get("userAccount");
        String targetUserName = (String) evaluation.get("userName");
        Map<String,Object> notify = new HashMap();
        Long notifyId = primaryKeyUtil.generatePrimaryKey("notify");
        notify.put("notifyId",notifyId);
        notify.put("internshipId",evaluation.get("internshipId"));
        notify.put("sendAccount",params.get("userAccount"));
        notify.put("targetAccount",targetAccount);
        notify.put("sendType",1);
        notify.put("targetType",1);
        notify.put("notifyType",1);
        notify.put("creat_account",params.get("userAccount"));
        notify.put("content",targetUserName+"点赞了你的评论");
        notifyMapper.addNotify(notify);
        evaluationMapper.addLikeNum(params);
        return result;
    }
    @Override
    public Result cancelLikeNum(Map params) {
        Result result = new Result();
        evaluationMapper.cancelLikeNum(params);
        return result;
    }

    Map getEvaluationChildren(Map personEvaluation){
       Long personEvaluationId = (Long) personEvaluation.get("evaluationId");
        List<Map> evaluationChildren = evaluationMapper.getEvaluationChildren(personEvaluationId);
        if(!CollectionUtils.isEmpty(evaluationChildren)){
            for(Map evaluation:evaluationChildren){
                getEvaluationChildren(evaluation);
            }
            personEvaluation.put("childrenReply",evaluationChildren);
        }
        return personEvaluation;
    }
}
